package com.ggb.grpc;

import io.grpc.Grpc;
import io.grpc.Server;
import io.grpc.ServerBuilder;

import java.io.IOException;

public class ProtoServerTest {
    private Server server;

    private void start() throws IOException {
        this.server = ServerBuilder.forPort(8899).addService(new StudentServiceGrpcImpl()).build().start();
        System.out.println("started!");

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("关闭Jvm");
            ProtoServerTest.this.stop();
        }));
    }

    private void stop() {
        if (null != this.server) {
            System.out.println("执行了吗？");
            this.server.shutdown();
        }
    }

    private void awaitTermination() throws InterruptedException {
        if (null != this.server) {
            this.server.awaitTermination();
        }
    }

    public static void main(String[] args) {
        ProtoServerTest serverTest = new ProtoServerTest();
        System.out.println("到这？");
        try {
            serverTest.start();
//            System.exit(0);
            serverTest.awaitTermination();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
